package com.hstc.controller;

import com.alibaba.cloud.ai.dashscope.audio.DashScopeSpeechSynthesisOptions;
import com.alibaba.cloud.ai.dashscope.audio.synthesis.SpeechSynthesisModel;
import com.alibaba.cloud.ai.dashscope.audio.synthesis.SpeechSynthesisPrompt;
import com.alibaba.cloud.ai.dashscope.audio.synthesis.SpeechSynthesisResponse;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.util.UUID;


@RestController
public class ChatClientController {
    public static final String BAILIAN_VOICE_MODEL = "cosyvoice-v2";
    public static final String BAILIAN_VOICE_TIMBER = "longyingcui";
    @Resource
    private SpeechSynthesisModel speechSynthesisModel;

    @GetMapping("t2i/voice")
    public String image(@RequestParam(name = "msg",defaultValue = "温馨提醒，支付宝到账100万元请注意查收") String msg){
        String filePath = "D:\\"+ UUID.randomUUID()+".mp3";
//        1.语音参数设置
        DashScopeSpeechSynthesisOptions options = DashScopeSpeechSynthesisOptions.builder()
                .model(BAILIAN_VOICE_MODEL)
                .voice(BAILIAN_VOICE_TIMBER)
                .build();
//        2.调用大模型语音生成对象
        SpeechSynthesisResponse call = speechSynthesisModel.call(new SpeechSynthesisPrompt(msg, options));
//        3.字节流语言转换
        ByteBuffer audio = call.getResult().getOutput().getAudio();
//        4.文件生成
        try (FileOutputStream fileOutputStream = new FileOutputStream(filePath)){
            fileOutputStream.write(audio.array());
        } catch (Exception e){
            System.out.println(e.getMessage());
        }
//        5.生成路径
        return filePath;
    }
}
